<template>
  <div class="user-list-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户列表</span>
          <el-button type="primary" @click="handleCreate">新增用户</el-button>
        </div>
      </template>
      
      <el-table :data="userList" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="email" label="邮箱" width="200" />
        <el-table-column prop="role" label="角色" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === '启用' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { userApi } from '../api'
import { useUserStore } from '../stores/modules'

export default {
  name: 'UserList',
  setup() {
    const userStore = useUserStore()
    const loading = ref(false)
    const userList = ref([])
    
    const pagination = reactive({
      page: 1,
      size: 10,
      total: 0
    })
    
    // 获取用户列表
    const fetchUserList = async () => {
      try {
        loading.value = true
        const res = await userApi.getUserList({
          page: pagination.page,
          size: pagination.size
        })
        
        userList.value = res.data.list
        pagination.total = res.data.total
      } catch (error) {
        ElMessage.error('获取用户列表失败: ' + error.message)
      } finally {
        loading.value = false
      }
    }
    
    // 处理分页大小变化
    const handleSizeChange = (val) => {
      pagination.size = val
      fetchUserList()
    }
    
    // 处理当前页变化
    const handleCurrentChange = (val) => {
      pagination.page = val
      fetchUserList()
    }
    
    // 处理新增用户
    const handleCreate = () => {
      ElMessage.info('新增用户')
    }
    
    // 处理编辑用户
    const handleEdit = (row) => {
      ElMessage.info(`编辑用户: ${row.name}`)
    }
    
    // 处理删除用户
    const handleDelete = (row) => {
      ElMessageBox.confirm(`确定要删除用户 ${row.name} 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await userApi.deleteUser(row.id)
          ElMessage.success('删除成功')
          fetchUserList()
        } catch (error) {
          ElMessage.error('删除失败: ' + error.message)
        }
      }).catch(() => {
        // 取消删除
      })
    }
    
    // 组件挂载时获取数据
    onMounted(() => {
      fetchUserList()
    })
    
    return {
      loading,
      userList,
      pagination,
      fetchUserList,
      handleSizeChange,
      handleCurrentChange,
      handleCreate,
      handleEdit,
      handleDelete
    }
  }
}
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>